jQuery(document).ready(function() {
	
	//Menus
	jQuery('#main-nav ul > li > .mega-menu-block, #main-nav ul > li > ul, #main-nav ul > li > ul > li > ul, #main-nav ul > li > ul > li > ul> li > ul, .top-menu  ul > li > ul, .top-menu  ul > li > ul > li > ul, .top-menu  ul > li > ul > li > ul> li > ul ').parent('li').addClass('parent-list');
	jQuery('.parent-list').find("a:first").append(' <span class="sub-indicator"></span>');
	
	jQuery("#main-nav li , .top-menu li").each(function(){	
		var $sublist = jQuery(this).find('ul:first:not(.mega-menu-content ul.sub-menu), .mega-menu-block');		
		jQuery(this).hover(function(){	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(200, function(){
				jQuery(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function(){	
			$sublist.stop().slideUp(200, function()	{	
				jQuery(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});

	
// User Rate functions
	jQuery(document).on('mousemove', '.user-rate-active' , function (e) {
		var rated = jQuery(this);
		if( rated.hasClass('rated-done') ){
			return false;
		}
		if (!e.offsetX){
			e.offsetX = e.clientX - jQuery(e.target).offset().left;
		}
		var offset = e.offsetX + 4;
		if (offset > 100) {
			offset = 100;
		}
		rated.find('.user-rate-image span').css('width', offset + '%');
		var score = Math.floor(((offset / 10) * 5)) / 10;
		if (score > 5) {
			score = 5;
		}
	});
	jQuery(document).on('click', '.user-rate-active' , function (e) {
		var rated = jQuery(this);
		if( rated.hasClass('rated-done') ){
			return false;
		}
		var gg = rated.find('.user-rate-image span').width();
		rated.find('.user-rate-image').hide();
		rated.append('<span class="tie-rate-load"></span>');
		if (gg > 100) {
			gg = 100;
		}
		ngg = (gg*5)/100;
		var post_id = rated.attr('data-id');
		var numVotes = rated.parent().find('.taq-count').text();
		
		jQuery.post(tie.ajaxurl, { action:'tie_rate_post' , post:post_id , value:ngg}, function(data) {
		var post_rateed = '.rate-post-'+post_id;
			jQuery( post_rateed ).addClass('rated-done').attr('data-rate',gg);
			rated.find('.user-rate-image span').width(gg+'%');
			
			jQuery(".tie-rate-load").fadeOut(function () {

				rated.parent().find('.taq-score').html( ngg );
				
				if( (jQuery(rated.parent().find('.taq-count'))).length > 0 ){
					numVotes =  parseInt(numVotes)+1;
					rated.parent().find('.taq-count').html(numVotes);
				}else{
					rated.parent().find('small').hide();
				}
				rated.parent().find('strong').html(tie.your_rating);
				
				rated.find('.user-rate-image').fadeIn();
			});
		}, 'html');
		return false;
	});
	jQuery(document).on('mouseleave', '.user-rate-active' , function (e) {
		var rated = jQuery(this);
		if( rated.hasClass('rated-done') ){
			return false;
		}
		var post_rate = rated.attr('data-rate');
		rated.find(".user-rate-image span").css('width', post_rate + '%');
	});

	// jQuery Mobile Menu
	var topMenu = jQuery('.top-menu ul.menu');
		if (topMenu.length) { topMenu.mobileMenu({  defaultText: tievar.go_to,  className: 'top-menu-mob'});}
	
	var mainMenu = jQuery('#main-nav ul.menu');
		if (mainMenu.length) {mainMenu.mobileMenu({  defaultText: tievar.go_to,	 className: 'main-menu-mob'	});	}
	
});

jQuery(window).load(function(){
	if(jQuery('.fixed-enabled').length>0){
		var headerHeight = jQuery('.fixed-enabled').offset().top+50 ;
		var mainNav = jQuery('.fixed-enabled');
		jQuery(window).scroll(function(){
			var scrollY=jQuery(window).scrollTop();
			if(scrollY > headerHeight){
				mainNav.addClass('fixed-nav');
			}else if(scrollY < headerHeight){
				mainNav.removeClass('fixed-nav');
			}
		});
	}
});

// jQuery Mobile Menu
(function($){ $.fn.mobileMenu = function(options) { var defaults = {defaultText: 'Navigate to...',className: 'select-menu',	subMenuClass: 'sub-menu',subMenuDash: '&nbsp; &nbsp; &ndash;'},settings = $.extend( defaults, options ),el = $(this);	this.each(function(){el.find('ul').addClass(settings.subMenuClass);	$('<select />',{'id' : settings.className}).insertAfter( el );	$('<option />', {"value": '#',	"text"	: settings.defaultText}).appendTo( '#' + settings.className );	el.find('a:not(.mega-menu-link)').each(function(){var $this 	= $(this),optText = '&nbsp;' + $this.text(),optSub	= $this.parents( '.' + settings.subMenuClass ),	len	= optSub.length,dash;	if( $this.parents('ul').hasClass( settings.subMenuClass ) ) {dash = Array( len+1 ).join( settings.subMenuDash );optText = dash + optText;}	$('<option />', {"value": this.href,"html"	: optText,"selected" : (this.href == window.location.href)}).appendTo( '#' + settings.className );});	$('#' + settings.className).change(function(){var locations = $(this).val();if( locations !== '#' ) {window.location.href = $(this).val();};});	});	return this;};})(jQuery);